#!/usr/bin/env ruby

require 'json'
require 'rest-client'

LKP_SRC = ENV['LKP_SRC'] || File.direname(File.dirname(File.realpath($PROGRAM_NAME)))

if ENV['RESULT_ROOT']
    fn = File.join ENV['RESULT_ROOT'], 'eulermaker_build_project_result'
elsif ARGV[0]
    fn = ARGV[0]
end

stats_json = File.read(fn)
stats = JSON::parse(stats_json)

def dfs_parse(data, prefix)
  data.each do |key, value|
    if value.is_a? Hash
      if key.split('.')[-1] == "build_packages"
        puts ".result.packages_list: #{value.keys}"
      end

      dfs_parse(value, "#{prefix}.#{key}")
    elsif value.is_a? Array
      normalized_value = Array.new
      value.each do |el|
        if el.is_a? Hash
          normalized_value << el.to_json
        else
          normalized_value << el
        end
      end
      puts "#{prefix}.#{key}: #{normalized_value}"
    elsif value.is_a? String
      puts "#{prefix}.#{key}: #{value}" unless value.empty?
    else
      puts "#{prefix}.#{key}: #{value}" unless value.nil?
    end
  end
end

puts ".result.build_id: 0"
dfs_parse(stats, ".result")
